package com.ptbuy.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.OpenableColumns;

import java.io.File;

public class Constants {


    public static String IP = "1.95.80.238";//正式
//        public static String IP = "192.168.1.104:8081";//本地测试
    public static String API = "http:" + File.separator + File.separator + IP + "/ptbuyht";//正式
//    public static String API = "http:" + File.separator + File.separator + IP;//本地正式

    public static String dbName = "ptbuy.db";
    //数据库路径
    public static String dbPath = Environment.getDataDirectory().getAbsolutePath() + File.separator + "data" + File.separator + "com.ptbuy" + File.separator + "db";
//    public static String dbPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "ptbuy";

    public static String apkPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "ptbuy";


    public static String multPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "ptbuy" + File.separator + "mult" + File.separator;

    public static String documentPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "ptbuy" + File.separator + "document" + File.separator;

    public static String getMultPath() {
        if (!new File(multPath).exists()) {
            new File(multPath).mkdirs();
        }
        //创建成功
        return multPath;
    }

    public static String getDocument() {
        if (!new File(documentPath).exists()) {
            new File(documentPath).mkdirs();
        }
        //创建成功
        return documentPath;
    }

    //导出文件存放路径
    public static String exportFilePath = Environment.getExternalStorageDirectory() + File.separator + "ptbuy" + File.separator + "export" + File.separator;

    public static String getExportFilePath() {
        if (!new File(exportFilePath).exists()) {
            new File(exportFilePath).mkdirs();
        }
        //创建成功
        return exportFilePath;
    }

    //更新专用

    //查询app版本是否有更新
    public static String UPDATE_URL = "http:" + File.separator + File.separator + IP + "/api" + File.separator + "app" + File.separator + "latest?type=ptBuy";

    public static String PACKAGE_NAME = "com.ptbuy";

    public static Context CurrentContext;

    public static String VERSION_ID = "EMPTY";
    public static String DOWNLOAD_URL = "EMPTY";


    /**
     * 删除单个文件
     *
     * @param filePath 被删除文件的文件名
     * @return 文件删除成功返回true，否则返回false
     */
    public static boolean deleteFile(String filePath) {
        try {
            File file = new File(filePath);
            if (file.isFile() && file.exists()) {
                return file.delete();
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * Uri转换位真实路径
     *
     * @param context
     * @param uri
     * @return
     */
    public static String getPathFromUri(Context context, Uri uri) {
        if (uri == null) {
            return null;
        }

        if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
            return uri.getPath();
        } else if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
            String authority = uri.getAuthority();

            if (authority.startsWith("com.android.externalstorage")) {
                return Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + uri.getPath().split(":")[1];
            } else {
                String id = DocumentsContract.getDocumentId(uri);
                String[] split = id.split(":");
                if (id.startsWith("raw:")) {
                    return id.replaceFirst("raw:", "");
                }
                String idStr = id;
                if (authority.startsWith("com.android.providers")) {
                    if (split.length > 1) {
                        idStr = split[1];
                    }
                }

                ContentResolver contentResolver = context.getContentResolver();
                Cursor cursor = contentResolver.query(MediaStore.Files.getContentUri("external"),
                        new String[]{MediaStore.Files.FileColumns.DATA},
                        "_id=?",
                        new String[]{idStr}, null);
                if (cursor != null) {
                    cursor.moveToFirst();
                    try {
                        int idx = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);

                        return cursor.getString(idx);
                    } catch (Exception e) {
                    } finally {
                        cursor.close();
                    }
                }
            }
        }
        return null;
    }


    // 辅助方法：获取文件名
    public static String getFileName(Context context, Uri uri) {
        if (uri == null) {
            return null;
        }
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        int index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        String name = cursor.getString(index);
        cursor.close();
        return name;
    }
}
